<template>
  <div id="login_container">
    <HelloWorld msg="后台管理系统"></HelloWorld>
    <el-form
      ref="loginFormRef"
      :model="state.loginForm"
      status-icon
      :rules="state.rules"
      label-width="60px"
      class="login_form"
      size="mini"
    >
      <el-form-item label="账号" prop="us">
        <el-input v-model="state.loginForm.us" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="ps">
        <el-input v-model="state.loginForm.ps" type="password" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">确定</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, unref, ref } from 'vue'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
import { login } from '@/api/user'
import { setLocal } from '@/utils/local'
import HelloWorld from '@/components/HelloWorld.vue' // Vetur(1192)报错，原因是Vetur暂不支持ts  更换支持ts的语法高亮插件 Volar 以取代 Vetur

export default defineComponent({
  name: 'Login',
  setup() {
    const router = useRouter()
    const loginFormRef = ref()
    const validateUs = (rule: any, value: string, callback: any) => {
      if (!value || value === ' ') {
        callback(new Error('请输入邮箱'))
      } else {
        const exp = /\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/
        if (!exp.test(value)) {
          callback(new Error('请输入正确的邮箱'))
        } else {
          callback()
        }
      }
    }
    const state = reactive({
      loginForm: {
        us: 'ceshi@ceshi.com',
        ps: 'ceshi'
      },
      rules: {
        us: [{ required: true, validator: validateUs, trigger: 'blur' }],
        ps: [{ required: true, message: '请输入密码', trigger: 'blur' }]
      }
    })
    const submitForm = () => {
      unref(loginFormRef).validate((valid: boolean) => {
        if (!valid) return
        login(state.loginForm)
          .then((res) => {
            if (res.data.code === 200) {
              ElMessage.success('登录成功')
              setLocal('token', res.data.token)
              router.push('/home')
            } else {
              ElMessage.error(res.data.msg)
            }
          })
          .catch((err) => {
            console.log('err: ', err)
          })
      })
    }
    const resetForm = () => {
      unref(loginFormRef).resetFields()
    }
    return {
      loginFormRef,
      state,
      submitForm,
      resetForm
    }
  },
  components: { HelloWorld }
})
</script>

<style lang="less" scoped>
#login_container {
  width: 400px;
  height: auto;
  margin: auto;
  background: #ccc;
  position: absolute;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  .login_form {
    bottom: 0;
    width: 100%;
    padding: 0 45px 20px;
    box-sizing: border-box;
    .ivu-form-item {
      margin-right: 0;
    }
    .sendBtn {
      padding: 0px;
      position: absolute;
      right: -35px;
      top: 0;
    }
  }
}
</style>
